<?php

define("CORE_PLUGINS_FOLDER", "/include/plugins/");
define("CORE_BBPLUGINS_FOLDER", "/include/facePlugins/");
define("CORE_SNIPPETS_FOLDER", "/include/snippets/");
define("CORE_CONFIG", "/cfg/config.php");

include_once "common.php";
include_once "iplugin.php";
include_once "smarty.class.php";
include_once "iframework.php";
include_once("mailer/class.phpmailer.php");

class Core
{
    var $version;
    var $plugins;
    var $snippets;
    var $config;
    var $framework;
    var $properties;
    var $static_values;

    function Core()
    {
        $odir = getcwd(); $tmp = dirname(__FILE__);
        chdir($tmp); chdir("..");
        $pth = getcwd(); chdir($odir);

        $this->config = parse_ini_file($pth . CORE_CONFIG, 1);
        $this->config["file_root"] = $pth;
        $this->config["system"]["file_root"] = $pth;
        $this->config["http_root"] = $this->config["system"]["http_root"];
        $this->config["www_name"] = $this->config["system"]["www_name"];
        $this->config["info_email"] = $this->config["system"]["info_email"];
        $this->plugins = null;
        $this->snippets = null;
        $this->framework = null;
        $this->properties = array();
        $this->static_values = array();
        $this->setProperty("core", $this);
    }


    function initialize()
    {
        $this->locateSnippets();

        if(null != $this->framework)
        {
            $frm = &$this->framework;
            $frm->initialize();
//          $core = &getCoreInstance();
//          $core->framework = $frm;

            $arr = $frm->plugins_hierarhy;
            $this->setValues("plugin_path", $arr);
        }
    }
    
    function shutdown()
    {
        if(null != $this->framework)
        {
            $this->framework->shutdown();
            unset($this->framework);
            $this->framework = null;
        }
        unset($this->snippets);
        $this->snippets = null;
    }

    function locateSnippets()
    {
        $plg = array();
        if ($handle = opendir($this->config["file_root"] . CORE_SNIPPETS_FOLDER))
        {
            // This is the correct way to loop over the directory.
            while (false !== ($file = readdir($handle)))
            {
                if((TRUE == is_dir($this->config["file_root"] . CORE_SNIPPETS_FOLDER."/".$file))
                    && (".." != $file)
                    && ("." != $file)
                    && ($file[0]!='.')
                    )
                {
                    $plg[] = $file;
                }
            }
            closedir($handle);
        } // end if
        
        // -- Add plugins to the pool
        foreach ($plg as $tag)
        {
            //$this->plugins[] = $p;
            include_once($this->config["file_root"] . CORE_SNIPPETS_FOLDER . "/" . $tag . "/" . $tag. ".php");
            $obj = null;
            eval("\$obj = &{$tag}_Export();");
            $this->snippets[$tag] = $obj;
        }
    }

    function setFramework($frm)
    {
        if(null != $this->framework)
        {
            $this->framework->shutdown();
            unset($this->framework);
            $this->framework = null;
        }
        
        if(null != $frm)
        {
            $this->framework = $frm;
        }
        $this->setProperty("framework", $frm);
    }
    
    function getProperty($tag)
    {
        if(isset($this->properties[$tag]))
        {
            return $this->properties[$tag];
        }
        return null;
    }
    
    function setProperty($tag, &$value)
    {
        if(isset($this->properties[$tag]))
        {
            unset ($this->properties[$tag]);
        }
        $this->properties[$tag] = $value;
    }

    function getValues($tag)
    {
        if(isset($this->static_values[$tag]))
        {
            return $this->static_values[$tag];
        }
        return null;
    }
    
    function setValues($tag, $value)
    {
        if(isset($this->static_values[$tag]))
        {
            unset ($this->static_values[$tag]);
        }
        $this->static_values[$tag] = $value;
    }


    function getSnippet($tag)
    {
        if(isset($this->snippets[$tag]))
        {
            return $this->snippets[$tag];
        }
        return  null;
    }



    function getGET()
    {
        return $_GET;
    }
    
    function getPathArguments()
    {
        $path = array();
        if("" != $_GET["path"])
        {
            $path = strSplit(",", $_GET["path"]);
        }
        return $path;
    }
    
    function getPluginPath($pname)
    {
        $pthtree = $this->getValues("plugin_path");
//      debug($pthtree, "DHD");
        return isset($pthtree[$pname]) ? $pthtree[$pname] : "";
    }
    
    
    function doAction()
    {
        if(null != $this->framework)
        {
            return $this->framework->doAction($this->getPathArguments());
        }
        return true;
    }

    function render()
    {
        if(null != $this->framework)
        {
            return $this->framework->render($this->getPathArguments());
        }
        return "";
    }

};


global $core;
$core =& new Core();

function &getCoreInstance()
{
    global $core;
    return $core;
}

?>